/**
 * 案件JS
 **/

/**
 * 添加
 * @param data 案件信息
 * @param callback 回调函数
 */
function saveCaseBasicInfoOffLine(data,callback){
	//立案日期默认系统时间
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_BASICINFO (" +
    			"CREATE_ID,CREATE_NAME,CREATE_TIME,MODIFY_ID,MODIFY_TIME,NOTE,BILL_ID," +
    			"CASE_NAME,CASE_NUMBER,CASE_SOURCE," +
    			"CCERT_ID,CERTIFICATE_ID_1,CERTIFICATE_ID_2,CLWGJYCS,CYRYWGJYCS," +
    			"EXECUTION_TYPE,FILLING_DATE,FILLING_PLACE,ILLEGAL_FACTS,ILLEGAL_LAW," +
    			"ILLEGAL_TYPE_CODE,JYLB,ORGAN_ADDRESS,ORGAN_CONTACTOR,ORGAN_NAME," +
    			"ORGAN_TEL,ORGAN_ZIPCODE,PARTY,PARTY_ADDRESS,PARTY_AGE," +
    			"PARTY_ECERTID,PARTY_ID_NO,PARTY_MANAGER,PARTY_SEX,PARTY_TEL," +
    			"PARTY_TYPE,PARTY_UNIT_POSITION,PARTY_ZIPCODE,PUNISH_AMOUNT,PUNISH_LAW," +
    			"REG_SYSTEM,SERIOUS_LEVEL,SHIP_CHECK_TON,SHIP_NAME,STAFF_1," +
    			"STAFF_2,STEP_TIME,VEHICLE_AXLES_NUMBER,VEHICLE_CHECK_TON,VEHICLE_GOODS_NAME," +
    			"VEHICLE_ID_COLOR,VEHICLE_SHIP_ID,VEHICLE_SHIP_TYPE,VEHICLE_TOTAL_WEIGHT,ZFML,ZFMLID," +
    			"ZIPAREA,DEPART_ID,DEPART_NAME,ORGAN_ID,CASE_TYPE,PUNISH_TPYE," +
    			"VEHICLE_LENGTH,VEHICLE_WIDTH,VEHICLE_HEIGHT,VEHICLE_REGISTER,CASECAUSEID," +
    			"CASECAUSENUMBER,DISCRETIONID,CASE_STEP,NEXT_CASE_STEP,REQUIRED) " +
    			"VALUES (?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?," +
    			"?,?,?,?,?,?,?)",
    			[data.createId==undefined ? '' : data.createId  ,data.createName ==undefined ? '' : data.createName,
    			 data.createTime==undefined ? '' : data.createTime,data.modifyId==undefined ? '' : data.modifyId,
    			 data.modifyTime==undefined ? '' : data.modifyTime,data.note==undefined ? '' : data.note,
    			 data.biliId==undefined ? '' : data.biliId,data.caseName==undefined ? '' : data.caseName,
    			 data.caseNumber==undefined ? '' : data.caseNumber,data.caseSource==undefined ? '' : data.caseSource,
    			 data.ccertId==undefined ? '' : data.ccertId,data.certificateId1==undefined ? '' : data.certificateId1,
    			 data.certificateId2==undefined ? '' : data.certificateId2,data.clwgjycs==undefined ? '' : data.clwgjycs,
    			 data.cyrywgjycs==undefined ? '' : data.cyrywgjycs,data.executionType==undefined ? '' : data.executionType,
    			 data.fillingDate==undefined ? '' : data.fillingDate,data.fillingPlace==undefined ? '' : data.fillingPlace,
    			 data.illegalFacts==undefined ? '' : data.illegalFacts,data.illegalLaw==undefined ? '' : data.illegalLaw,
    			 data.illegalTypeCode==undefined ? '' : data.illegalTypeCode,data.jylb==undefined ? '' : data.jylb,
    			 data.organAddress==undefined ? '' : data.organAddress,data.organContactor==undefined ? '' : data.organContactor,
    			 data.organName==undefined ? '' : data.organName,data.organTel==undefined ? '' : data.organTel,
    			 data.organZipCode==undefined ? '' : data.organZipCode,data.party==undefined ? '' : data.party,
    			 data.partyAddress==undefined ? '' : data.partyAddress,data.partyAge==undefined ? '' : data.partyAge,
    			 data.partyEcertId==undefined ? '' : data.partyEcertId,data.partyIdNo==undefined ? '' : data.partyIdNo,
    			 data.partyManager==undefined ? '' : data.partyManager,data.partySex==undefined ? '' : data.partySex,
    			 data.partyTel==undefined ? '' : data.partyTel, data.partyType==undefined ? '' : data.partyType,
    			 data.partyUnitPosition==undefined ? '' : data.partyUnitPosition,data.partyZipCode==undefined ? '' : data.partyZipCode,
    			 data.punishAmount==undefined ? '' : data.punishAmount,data.punishLaw==undefined ? '' : data.punishLaw,
    			 data.regSystem==undefined ? '' : data.regSystem,data.seriousLevel==undefined ? '' : data.seriousLevel,
    			 data.shipCheckTon==undefined ? '' : data.shipCheckTon,data.shipName==undefined ? '' : data.shipName,
    			 data.staff1==undefined ? '' : data.staff1, data.staff2==undefined ? '' : data.staff2,
    			 data.stepTime==undefined ? '' : data.stepTime,data.vehicleAxlesNumber==undefined ? '' : data.vehicleAxlesNumber,
    			 data.vehicleCheckTon==undefined ? '' : data.vehicleCheckTon,data.vehicleGoodsName==undefined ? '' : data.vehicleGoodsName,
    			 data.vehicleIdColor==undefined ? '' : data.vehicleIdColor,data.vehicleShipId==undefined ? '' : data.vehicleShipId,
    			 data.vehicleShipType==undefined ? '' : data.vehicleShipType,data.vehicleTotalWeight==undefined ? '' : data.vehicleTotalWeight,
    			 data.zfml==undefined ? '' : data.zfml,data.zfmlId==undefined ? '' : data.zfmlId,
    			 data.zipArea==undefined ? '' : data.zipArea,data.departId==undefined ? '' : data.departId,
    			 data.departName==undefined ? '' : data.departName,data.organId==undefined ? '' : data.organId,
    			 data.caseType==undefined ? '' : data.caseType,data.punishType==undefined ? '' : data.punishType,
    	    	 data.vehicleLength==undefined ? '' : data.vehicleLength,data.vehicleWidth==undefined ? '' : data.vehicleWidth,
    	    	 data.vehicleHeight==undefined ? '' : data.vehicleHeight,data.vehicleRegister==undefined ? '' : data.vehicleRegister,
    			 data.caseCauseId==undefined ? '' : data.caseCauseId,data.caseCauseNumber==undefined ? '' : data.caseCauseNumber,
    			 data.discretionId==undefined ? '' : data.discretionId,data.caseStep==undefined ? '' : data.caseStep,
    			 data.nextCaseStep==undefined ? '' : data.nextCaseStep,data.required==undefined ? '' : data.required],
       function (tx, result) {
    		data.id = result.insertId;
    		closeShade();
    	    message("案件信息登记成功");
    	    $('#case_submit_link').linkbutton('enable');
    	    if(typeof(callback) == 'function'){
    	    	callback(data,"#caseBasicInfoForm");
    	    }
    	    //保存 离线立案地址
    	    findCaseFillingPlaceByAddressAndOrganIdOffLine(data.fillingPlace,data.organId,data.organName,saveCaseFillingPlaceOffLine);
    	    //保存 离线 行政处罚种类
    	    findAdministrativePunishmentByNameAndOrganIdOffLine(data.punishType,data.punishType,data.organId,saveAdministrativePunishmentOffLine);
    	    //保存 执法人员
    	    if($.trim(data.staff1).length != 0){
    	    	findStaffByCertificateIdAndOrganIdOffLine(data.staff1,data.certificateId1,data.organId,data.organName,saveStaffOffLine);
    	    }
    	    if($.trim(data.staff2).length != 0){
    	    	findStaffByCertificateIdAndOrganIdOffLine(data.staff2,data.certificateId2,data.organId,data.organName,saveStaffOffLine);
    	    }
    	    //search();
    	    saveCaseBasicInfoClOffLine(data);
       },
	   function (tx, error) {
    	   closeShade();
	       message("案件信息登记失败");
	   });
    });
}

/**
 * 修改
 * @param data
 * @param callback
 */
function updateCaseBasicInfoOffLine(data,callback){
	db.transaction(function (tx) {
    	tx.executeSql("UPDATE CASE_BASICINFO SET " +
    			"MODIFY_ID=?," +
    			"MODIFY_TIME=?," +
    			"NOTE=?," +
    			"BILL_ID=?," +
    			"CASE_NAME=?," +
    			"CASE_NUMBER=?," +
    			"CASE_SOURCE=?," +
    			"CCERT_ID=?," +
    			"ORGAN_ID=?," +
    			"ORGAN_NAME=?," +
    			"CERTIFICATE_ID_1=?," +
    			"CERTIFICATE_ID_2=?," +
    			"CLWGJYCS=?," +
    			"CYRYWGJYCS=?," +
    			"EXECUTION_TYPE=?," +
    			"FILLING_PLACE=?," +
    			"ILLEGAL_FACTS=?," +
    			"ILLEGAL_LAW=?," +
    			"ILLEGAL_TYPE_CODE=?," +
    			"JYLB=?," +
    			"ORGAN_ADDRESS=?," +
    			"ORGAN_CONTACTOR=?," +
    			"ORGAN_TEL=?," +
    			"ORGAN_ZIPCODE=?," +
    			"PARTY=?," +
    			"PARTY_ADDRESS=?," +
    			"PARTY_AGE=?," +
    			"PARTY_ECERTID=?," +
    			"PARTY_ID_NO=?," +
    			"PARTY_MANAGER=?," +
    			"PARTY_SEX=?," +
    			"PARTY_TEL=?," +
    			"PARTY_TYPE=?," +
    			"PARTY_UNIT_POSITION=?," +
    			"PARTY_ZIPCODE=?," +
    			"PUNISH_AMOUNT=?," +
    			"PUNISH_LAW=?," +
    			"REG_SYSTEM=?," +
    			"SERIOUS_LEVEL=?," +
    			"SHIP_CHECK_TON=?," +
    			"SHIP_NAME=?," +
    			"STAFF_1=?," +
    			"STAFF_2=?," +
    			"STEP_TIME=?," +
    			"VEHICLE_AXLES_NUMBER=?," +
    			"VEHICLE_CHECK_TON=?," +
    			"VEHICLE_GOODS_NAME=?," +
    			"VEHICLE_ID_COLOR=?," +
    			"VEHICLE_SHIP_ID=?," +
    			"VEHICLE_SHIP_TYPE=?," +
    			"VEHICLE_TOTAL_WEIGHT=?," +
    			"ZIPAREA=?," +
    			"CASE_TYPE=?," +
    			"PUNISH_TPYE=?, " +
    			"DEPART_ID=?," +
    			"DEPART_NAME=?," +
    			"VEHICLE_LENGTH=?," +
    			"VEHICLE_WIDTH=?," +
    			"VEHICLE_HEIGHT=?," +
    			"VEHICLE_REGISTER=?, " +
    			"CASECAUSEID=?, " +
    			"CASECAUSENUMBER=?, " +
    			"DISCRETIONID=? " +
    			"WHERE CASE_BASICINFO_ID=? ",
    			[data.modifyId==undefined ? '' : data.modifyId,
    			 data.modifyTime==undefined ? '' : data.modifyTime,
				 data.note==undefined ? '' : data.note,
    			 data.biliId==undefined ? '' : data.biliId,
				 data.caseName==undefined ? '' : data.caseName,
				 data.caseNumber==undefined ? '' : data.caseNumber,
				 data.caseSource==undefined ? '' : data.caseSource,
    			 data.ccertId==undefined ? '' : data.ccertId,
				 data.organId==undefined ? '' : data.organId,
				 data.organName==undefined ? '' : data.organName,
				 data.certificateId1==undefined ? '' : data.certificateId1, 
				 data.certificateId2==undefined ? '' : data.certificateId2,
				 data.clwgjycs==undefined ? '' : data.clwgjycs,
    			 data.cyrywgjycs==undefined ? '' : data.cyrywgjycs,
				 data.executionType==undefined ? '' : data.executionType,
				 data.fillingPlace==undefined ? '' : data.fillingPlace,
    			 data.illegalFacts==undefined ? '' : data.illegalFacts,
				 data.illegalLaw==undefined ? '' : data.illegalLaw,
				 data.illegalTypeCode==undefined ? '' : data.illegalTypeCode,
				 data.jylb==undefined ? '' : data.jylb,
    			 data.organAddress==undefined ? '' : data.organAddress,
				 data.organContactor==undefined ? '' : data.organContactor,
				 data.organTel==undefined ? '' : data.organTel,
				 data.organZipCode==undefined ? '' : data.organZipCode,
				 data.party==undefined ? '' : data.party,
				 data.partyAddress==undefined ? '' : data.partyAddress,
				 data.partyAge==undefined ? '' : data.partyAge,
				 data.partyEcertId==undefined ? '' : data.partyEcertId,
				 data.partyIdNo==undefined ? '' : data.partyIdNo,
				 data.partyManager==undefined ? '' : data.partyManager,
				 data.partySex==undefined ? '' : data.partySex,
				 data.partyTel==undefined ? '' : data.partyTel, 
				 data.partyType==undefined ? '' : data.partyType,
				 data.partyUnitPosition==undefined ? '' : data.partyUnitPosition,
				 data.partyZipCode==undefined ? '' : data.partyZipCode,
				 data.punishAmount==undefined ? '' : data.punishAmount,
				 data.punishLaw==undefined ? '' : data.punishLaw,
				 data.regSystem==undefined ? '' : data.regSystem,
				 data.seriousLevel==undefined ? '' : data.seriousLevel,
    			 data.shipCheckTon==undefined ? '' : data.shipCheckTon,
				 data.shipName==undefined ? '' : data.shipName,
    			 data.staff1==undefined ? '' : data.staff1, 
				 data.staff2==undefined ? '' : data.staff2,
    			 data.stepTime==undefined ? '' : data.stepTime,
				 data.vehicleAxlesNumber==undefined ? '' : data.vehicleAxlesNumber,
    			 data.vehicleCheckTon==undefined ? '' : data.vehicleCheckTon,
				 data.vehicleGoodsName==undefined ? '' : data.vehicleGoodsName,
    			 data.vehicleIdColor==undefined ? '' : data.vehicleIdColor,
				 data.vehicleShipId==undefined ? '' : data.vehicleShipId,
    			 data.vehicleShipType==undefined ? '' : data.vehicleShipType,
				 data.vehicleTotalWeight==undefined ? '' : data.vehicleTotalWeight,
				 data.zipArea==undefined ? '' : data.zipArea,
				 data.caseType==undefined ? '' : data.caseType,
    			 data.punishType==undefined ? '' : data.punishType,
				 data.departId==undefined ? '' : data.departId,
				 data.departName==undefined ? '' : data.departName,
		    	 data.vehicleLength==undefined ? '' : data.vehicleLength,data.vehicleWidth==undefined ? '' : data.vehicleWidth,
		    	 data.vehicleHeight==undefined ? '' : data.vehicleHeight, data.vehicleRegister==undefined ? '' : data.vehicleRegister,
    			 data.caseCauseId==undefined ? '' : data.caseCauseId,
				 data.caseCauseNumber==undefined ? '' : data.caseCauseNumber,
		    	 data.discretionId==undefined ? '' : data.discretionId,
    			 data.id],
       function (tx, result) {
    		closeShade();
    	    message("案件信息修改成功");
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
    	   closeShade();
	       message("案件信息修改失败");
	   });
    });
	
}
/**
 * 通过案件ID获取案件信息
 * @param id 案件ID
 * @param callback 回调函数
 **/
function findCaseBasicInfoByIdOffLine(id,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_BASICINFO WHERE CASE_BASICINFO_ID = ? AND ISONLINE = '离线'", [parseInt(id)], 
		function (tx, result){ 
		    var len = result.rows.length;  
		    if(len == 0){
		    	message("案件不存在");
		    	return;
		    }
		    
		    if(len > 1){
		    	message("获取案件错误");
		    	return;
		    }
		    
		    var data = setCaseDataOffLine(result);
    	    if(typeof(callback) == 'function'){
    	    	callback(data[0],"#caseBasicInfoForm");
    	    }
    	    return data[0];
	    }, 
	    function(tx, ex) {
            message("案件不存在");
        });  
	});
}

/**
 * 通过案件ID查询案件表数据
 * @param id
 * @param callback
 */
function findCaseBasicInfoById(callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT COUNT(*) TOTAL FROM CASE_BASICINFO WHERE ISONLINE = '离线'", [], 
				function (tx, results){ 
			var len = results.rows.length;
			var count = 0;
		    for (var i = 0; i < len; i++){
		    	count = results.rows.item(i).TOTAL;
		    }
		    
		    if(count > 0){
				message("案件表中存在数据，请回传数据！");
				return;
			}
		    
			if(count == 0){
				dropTab();
				return;
			}
		});  
	});
}



/**
 * 离线待办事项
 * @param data
 * @param callback
 */

function toFindCountDo(callback){
	$("#th").empty();
	db.transaction(function (tx) {
		tx.executeSql("SELECT COUNT(*) TOTAL FROM CASE_BASICINFO WHERE ORGAN_ID = ? AND ISONLINE = '离线' AND CASE_STEP != '结案'",
				[organId],
				function (tx, result) {
					var len = result.rows.length;
					var count = 0;
				    for (var i = 0; i < len; i++){
				    	count = result.rows.item(i).TOTAL;
				    }
					if(count == 0){
						$("#th").hide();
						return;					
					}
					$("#th").append(count);

				});
				
	});
}
/**
 * 根据查询条件获取案件总数
 * @param condition 查询条件
 * @param page 分页
 * @param callback  回调函数
 **/
function findCaseBasicInfoTotalOffLine(condition,con,page){
	var sql = addCondition(condition);
	db.transaction(function (tx) { 
		tx.executeSql("SELECT COUNT(*) TOTAL FROM CASE_BASICINFO WHERE ORGAN_ID = ? AND ISONLINE = '离线'" + sql, [condition.organId], 
		function (tx, result){
		    var len = result.rows.length;
		    for (var i = 0; i < len; i++){
		    	page.data.total = result.rows.item(i).TOTAL;
		    }
		    /**
			 * 回调函数
			 */
			if(typeof(page.callback) == 'function'){
				page.callback();
			}
			$(con.datagrid).datagrid({
				border:false,
				fitColumns:true,
				checkbox:true,
				fit:true,
				remoteSort:false,
				rownumbers:false,
				singleSelect:con.singleSelect,
				toolbar: con.datagridtoolbar,
				pagination:con,
				loadFilter:pagerFilter}).datagrid('loadData', page.data);
	    }, 
	    function(tx, error) {
	    	message("获取案件信息列表失败:"+ error.message);
        });  
	});
}

/**
 * 根据查询条件案件列表
 * @param condition 查询条件
 * @param page 分页
 **/
function findAllCaseBasicInfoOffLine(condition,con,page){
	var sql = addCondition(condition);
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_BASICINFO WHERE ORGAN_ID = ? AND ISONLINE = '离线' " + sql +" ORDER BY CREATE_TIME DESC LIMIT ?,?", [condition.organId,(page.pageNumber)*page.pageSize,page.pageSize], 
		function (tx, result){
			page.data.rows = setCaseDataOffLine(result);
		    findCaseBasicInfoTotalOffLine(condition,con,page);
	    }, 
	    function(tx, error) {
	    	message("获取案件信息列表失败:"+ error.message);
        });  
	});
}

/**
 * 拼接sql语句
 * @param condition 查询条件
 **/
function addCondition(condition){
	
	var sqlWhere = "";
	
	if($.trim(condition.caseQuick).length != 0){
		sqlWhere += " AND ( CASE_NUMBER LIKE '%" + condition.caseQuick + "%'";
		sqlWhere += " OR FILLING_PLACE LIKE '%" + condition.caseQuick + "%'";
		sqlWhere += " OR PARTY LIKE '%" + condition.caseQuick + "%'";
		sqlWhere += " OR ZFML LIKE '%" + condition.caseQuick + "%'";
		sqlWhere += " OR ILLEGAL_TYPE_CODE LIKE '%" + condition.caseQuick + "%'";
		sqlWhere += " OR CASE_STEP LIKE '%" + condition.caseQuick + "%' )";
	}else{
		/**
		 * 案件编号
		 **/
		if($.trim(condition.caseNumber).length != 0){
			sqlWhere += " AND CASE_NUMBER LIKE '%" + condition.caseNumber + "%'";
		}
		
		/**
		 * 当事人
		 **/
		if($.trim(condition.party).length != 0){
			sqlWhere += " AND PARTY LIKE '%" + condition.party + "%'";
		}
		
		/**
		 * 立案时间-开始时间
		 **/
		if($.trim(condition.caseStartTime).length != 0){
			sqlWhere += " AND FILLING_DATE >= date('"+condition.caseStartTime+"')";
		}
		
		/**
		 * 立案时间-结束时间
		 **/
		if($.trim(condition.caseEndTime).length != 0){
			sqlWhere += " AND FILLING_DATE <= date('"+condition.caseEndTime+"')";
		}
		
		/**
		 * 案件环节
		 **/
		if($.trim(condition.caseStep).length != 0){
			sqlWhere += " AND CASE_STEP = '" + condition.caseStep + "'";
		}
	}

	
	/**
	 * 排除结案环节
	 **/
	if($.trim(condition.noJa).length != 0){
		sqlWhere += " AND CASE_STEP != '" + condition.noJa + "'";
	}
	
	return sqlWhere;
}

/**
 * 删除案件所有数据
 * @param callback
 **/
function deleteAllCaseBasicInfoOffLine(callback){
	db.transaction(function (tx) { 
		tx.executeSql('DELETE FROM CASE_BASICINFO', [], 
			function (tx, results){
				message("删除案件所有数据成功");
	    	    if(typeof(callback) == 'function'){
	    	    	callback(data);
	    	    }
			}, 
			function (tx, ex){
				message("删除案件所有数据失败");
			});  
	});
}

/**
 * 修改案件环节
 * @param id 案件ID
 * @param caseeStep 案件环节
 * @param callback  回调函数
 **/
function updateCaseBasicInfoCaseeStepOffLine(id,caseeStep,nextCaseeStep,required,callback){
	db.transaction(function (tx) {
		tx.executeSql('UPDATE CASE_BASICINFO SET CASE_STEP=?,NEXT_CASE_STEP=?,REQUIRED=? WHERE CASE_BASICINFO_ID = ?', [caseeStep,nextCaseeStep,required,id], 
			function (tx, results){
				message("案件环节修改成功");
	    		caseData.caseStep=caseeStep;
	    		caseData.nextCaseStep=nextCaseeStep;
	    		caseData.required=required;
				if (typeof(callback) == 'function')
		            callback();
				caseData.id=id;
				saveCaseBasicInfoClOffLine(caseData,initTooltip);
			}, 
			function (tx, ex){
				message("案件环节修改失败");
			});
	});
}

/**
 * 添加非必要环节修改案件环节
 * @param id 案件ID
 * @param caseeStep 案件环节
 * @param callback  回调函数
 **/
function updateCaseBasicInfoCaseeStepOffLine_(id,nextCaseeStep,item){
	db.transaction(function (tx) {
		tx.executeSql('UPDATE CASE_BASICINFO SET NEXT_CASE_STEP=? WHERE CASE_BASICINFO_ID = ?', [nextCaseeStep,id], 
			function (tx, results){
				message("案件环节修改成功");
				resetCaseStep(item);
			}, 
			function (tx, ex){
				message("案件环节修改失败");
			});
	});
}

/**
 * 修改案件的在线/离线状态
 * @param id 案件ID
 * @param onLine 在线/离线 
 * @param callback 回调函数
 **/
function updateCaseBasicInfoIsOnLineOffLine(id,onLine,callback){
	db.transaction(function (tx) { 
		tx.executeSql('UPDATE CASE_BASICINFO SET ISONLINE = ? WHERE CASE_BASICINFO_ID = ?', [onLine,parseInt(id)], 
				function (tx, results){
					message("案件在线/离线状态修改成功");
					if (typeof(callback) == 'function')
			            callback(new page());
				}, 
				function (tx, ex){
					message("案件在线/离线状态修改失败");
				});
	});
}

/**
 * 组装数据list
 * @param result
 * @returns {Array}
 */
function setCaseDataOffLine(result){
	var len = result.rows.length;
    var data = new Array();
	for (var i = 0; i < len; i++){
		var caseBasicInfo = {};
		caseBasicInfo.id = result.rows.item(i).CASE_BASICINFO_ID;
		caseBasicInfo.caseName = result.rows.item(i).CASE_NAME;
		caseBasicInfo.zfml = result.rows.item(i).ZFML;
		caseBasicInfo.zfmlId = result.rows.item(i).ZFMLID;
		caseBasicInfo.executionType   = result.rows.item(i).EXECUTION_TYPE;
		caseBasicInfo.caseNumber = result.rows.item(i).CASE_NUMBER;
		caseBasicInfo.fillingPlace = result.rows.item(i).FILLING_PLACE;
		caseBasicInfo.organName = result.rows.item(i).ORGAN_NAME;
		caseBasicInfo.organId = result.rows.item(i).ORGAN_ID;
		caseBasicInfo.departName = result.rows.item(i).DEPART_NAME;
		caseBasicInfo.departId = result.rows.item(i).DEPART_ID;
		caseBasicInfo.zipArea = result.rows.item(i).ZIPAREA;
		caseBasicInfo.organAddress = result.rows.item(i).ORGAN_ADDRESS;
		caseBasicInfo.organZipCode = result.rows.item(i).ORGAN_ZIPCODE;
		caseBasicInfo.organContactor = result.rows.item(i).ORGAN_CONTACTOR;
		caseBasicInfo.organTel = result.rows.item(i).ORGAN_TEL;
		caseBasicInfo.fillingDate = result.rows.item(i).FILLING_DATE;
		caseBasicInfo.partyType = result.rows.item(i).PARTY_TYPE;
		caseBasicInfo.party = result.rows.item(i).PARTY;
		caseBasicInfo.partySex = result.rows.item(i).PARTY_SEX;
		caseBasicInfo.partyAge = result.rows.item(i).PARTY_AGE;
		caseBasicInfo.partyTel = result.rows.item(i).PARTY_TEL;
		caseBasicInfo.partyIdNo = result.rows.item(i).PARTY_ID_NO;
		caseBasicInfo.partyAddress = result.rows.item(i).PARTY_ADDRESS;
		caseBasicInfo.partyZipCode = result.rows.item(i).PARTY_ZIPCODE;
		caseBasicInfo.partyUnitPosition = result.rows.item(i).PARTY_UNIT_POSITION;
		caseBasicInfo.partyManager = result.rows.item(i).PARTY_MANAGER;
		caseBasicInfo.partyEcertId = result.rows.item(i).PARTY_ECERTID;
		caseBasicInfo.jylb = result.rows.item(i).JYLB;
		caseBasicInfo.vehicleShipId = result.rows.item(i).VEHICLE_SHIP_ID;
		caseBasicInfo.vehicleIdColor = result.rows.item(i).VEHICLE_ID_COLOR;
		caseBasicInfo.vehicleShipType = result.rows.item(i).VEHICLE_SHIP_TYPE;
		caseBasicInfo.ccertId = result.rows.item(i).CCERT_ID;
		caseBasicInfo.illegalTypeCode = result.rows.item(i).ILLEGAL_TYPE_CODE;
		caseBasicInfo.punishAmount = result.rows.item(i).PUNISH_AMOUNT;
		caseBasicInfo.illegalLaw = result.rows.item(i).ILLEGAL_LAW;
		caseBasicInfo.punishLaw = result.rows.item(i).PUNISH_LAW;
		caseBasicInfo.illegalFacts = result.rows.item(i).ILLEGAL_FACTS;
		caseBasicInfo.staff1 = result.rows.item(i).STAFF_1;
		caseBasicInfo.certificateId1 = result.rows.item(i).CERTIFICATE_ID_1;
		caseBasicInfo.staff2 = result.rows.item(i).STAFF_2;
		caseBasicInfo.certificateId2 = result.rows.item(i).CERTIFICATE_ID_2;
		caseBasicInfo.biliId = result.rows.item(i).BILL_ID;
		caseBasicInfo.regSystem = result.rows.item(i).REG_SYSTEM;
		caseBasicInfo.caseSource = result.rows.item(i).CASE_SOURCE;
		caseBasicInfo.seriousLevel = result.rows.item(i).SERIOUS_LEVEL;
		caseBasicInfo.clwgjycs = result.rows.item(i).CLWGJYCS;
		caseBasicInfo.cyrywgjycs = result.rows.item(i).CYRYWGJYCS;
		caseBasicInfo.vehicleGoodsName = result.rows.item(i).VEHICLE_GOODS_NAME;	
		caseBasicInfo.vehicleAxlesNumber = result.rows.item(i).VEHICLE_AXLES_NUMBER;	
		caseBasicInfo.vehicleCheckTon = result.rows.item(i).VEHICLE_CHECK_TON;	
		caseBasicInfo.vehicleTotalWeight = result.rows.item(i).VEHICLE_TOTAL_WEIGHT;
		caseBasicInfo.vehicleLength = result.rows.item(i).VEHICLE_LENGTH;
		caseBasicInfo.vehicleWidth = result.rows.item(i).VEHICLE_WIDTH;
		caseBasicInfo.vehicleHeight = result.rows.item(i).VEHICLE_HEIGHT;
		caseBasicInfo.vehicleRegister = result.rows.item(i).VEHICLE_REGISTER;
		caseBasicInfo.shipName = result.rows.item(i).SHIP_NAME;	
		caseBasicInfo.shipCheckTon = result.rows.item(i).SHIP_CHECK_TON;	
		caseBasicInfo.caseType = result.rows.item(i).CASE_TYPE;	
		caseBasicInfo.punishType = result.rows.item(i).PUNISH_TPYE;
		caseBasicInfo.stepTime = result.rows.item(i).STEP_TIME;
		caseBasicInfo.caseStep = result.rows.item(i).CASE_STEP;
		caseBasicInfo.nextCaseStep = result.rows.item(i).NEXT_CASE_STEP;
		caseBasicInfo.required = result.rows.item(i).REQUIRED;
		caseBasicInfo.createId = result.rows.item(i).CREATE_ID;
		caseBasicInfo.createName = result.rows.item(i).CREATE_NAME;
		caseBasicInfo.createTime = result.rows.item(i).CREATE_TIME; 
		caseBasicInfo.modifyId = result.rows.item(i).MODIFY_ID;
		caseBasicInfo.modifyTime = result.rows.item(i).MODIFY_TIME; 
		caseBasicInfo.note = result.rows.item(i).NOTE; 
		caseBasicInfo.caseCauseId = result.rows.item(i).CASECAUSEID; 
		caseBasicInfo.caseCauseNumber = result.rows.item(i).CASECAUSENUMBER; 
		caseBasicInfo.discretionId = result.rows.item(i).DISCRETIONID; 
		caseBasicInfo.isOnLine = result.rows.item(i).ISONLINE; 
	    data.push(caseBasicInfo);
    }
	return data;
}

/**
 * 添加 案件处理记录
 * @param data 案件信息
 * @param callback 回调函数
 */
function saveCaseBasicInfoClOffLine(data,callback){
	db.transaction(function (tx) {
    	tx.executeSql("INSERT INTO CASE_BASICINFOCL (" +
    			"CREATE_ID,CREATE_NAME,CREATE_TIME,FILLING_DATE," +
    			"CASE_NAME,CASE_NUMBER,CASE_STEP,NEXT_CASE_STEP,REQUIRED,STAFF_1, STAFF_2,CASE_BASICINFO_ID) " +
    			"VALUES (?,?,?,?,?,?,?," +
    			"?,?,?,?,?)",
    			[data.createId==undefined ? '' : data.createId  ,
				 data.createName ==undefined ? '' : data.createName,
    			 data.createTime==undefined ? '' : data.createTime,
    			 data.fillingDate==undefined ? '' : data.fillingDate,
				 data.caseName==undefined ? '' : data.caseName,
				 data.caseNumber==undefined ? '' : data.caseNumber,
				 data.caseStep==undefined ? '' : data.caseStep,
				 data.nextCaseStep==undefined ? '' : data.nextCaseStep,
				 data.required==undefined ? '' : data.required,
    			 data.staff1==undefined ? '' : data.staff1, 
				 data.staff2==undefined ? '' : data.staff2,
				 data.id==''? caseBasicInfoId : data.id],
       function (tx, result) {
    	    if(typeof(callback) == 'function'){
    	    	callback(data);
    	    }
       },
	   function (tx, error) {
	   });
    });
}


/**
 * 通过案件ID获取案件处理信息
 * @param id 案件ID
 * @param caseStep 案件环节
 * @param callback 回调函数
 **/
function findCaseBasicInfoClByIdOffLine(id,linkNameId,caseStep,callback){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT * FROM CASE_BASICINFOCL WHERE CASE_BASICINFO_ID = ? AND CASE_STEP=? AND ISONLINE = '离线'", [parseInt(id),caseStep], 
		function (tx, result){ 
		    var len = result.rows.length;  
		    if(len == 0){
		    	return;
		    }
		    
		    var data = setCaseClDataOffLine(result);
		    if(data ==null)
		    	return;
    	    if(typeof(callback) == 'function'){
    	    	callback(data,linkNameId);
    	    }
	    }, 
	    function(tx, ex) {
        });  
	});
}

/**
 * 通过案件ID与案件环节必要性查询案件处理记录
 * @param id 案件ID
 * @param required 案件环节:0非必要环节、1必要环节
 * @param callback 回调函数
 **/
function findCaseBasicInfoClByIdAndRequiredOffLine(id,required,callback,data){
	db.transaction(function (tx) { 
		tx.executeSql("SELECT DISTINCT CASE_STEP,NEXT_CASE_STEP,REQUIRED FROM CASE_BASICINFOCL WHERE CASE_BASICINFO_ID = ? AND REQUIRED=? AND ISONLINE = '离线'", [parseInt(id),required], 
		function (tx, result){ 
		    var len = result.rows.length; 
		    var tempdata = new Array();
			for (var i = 0; i < len; i++){
				var caseBasicInfoCl = {};
				caseBasicInfoCl.caseStep = result.rows.item(i).CASE_STEP;
				caseBasicInfoCl.nextCaseStep = result.rows.item(i).NEXT_CASE_STEP;
				caseBasicInfoCl.required = result.rows.item(i).REQUIRED;
				tempdata.push(caseBasicInfoCl);
			}
			var tempCaseStep=setCaseStep(data,tempdata);
	    	if(typeof(callback)=='function'){
	    		callback(tempCaseStep);
	    	}
	    }, 
	    function(tx, ex) {
        });  
	});
}

/**
 * 组装数据list
 * @param result
 * @returns {Array}
 */
function setCaseClDataOffLine(result){
	var len = result.rows.length;
	if(len == 0){
		return null;
	}
    var data = new Array();
	for (var i = 0; i < len; i++){
		var caseBasicInfoCl = {};
		caseBasicInfoCl.caseBasicInfoid = result.rows.item(i).CASE_BASICINFO_ID;
		caseBasicInfoCl.caseName = result.rows.item(i).CASE_NAME;
		caseBasicInfoCl.caseNumber = result.rows.item(i).CASE_NUMBER;
		caseBasicInfoCl.fillingDate = result.rows.item(i).FILLING_DATE;
		caseBasicInfoCl.staff1 = result.rows.item(i).STAFF_1;
		caseBasicInfoCl.staff2 = result.rows.item(i).STAFF_2;
		caseBasicInfoCl.caseStep = result.rows.item(i).CASE_STEP;
		caseBasicInfoCl.nextCaseStep = result.rows.item(i).nextCaseStep;
		caseBasicInfoCl.required = result.rows.item(i).REQUIRED;
		caseBasicInfoCl.createId = result.rows.item(i).CREATE_ID;
		caseBasicInfoCl.createName = result.rows.item(i).CREATE_NAME;
		caseBasicInfoCl.createTime = result.rows.item(i).CREATE_TIME; 
		caseBasicInfoCl.isOnLine = result.rows.item(i).ISONLINE; 
	    data.push(caseBasicInfoCl);
    }
	return data;
}

/**
 * 生成六位数的文书序号
 * @param num
 * @param n
 * @returns
 */
function pad(num,n) {  
    var len = num.toString().length;
    while(len < n) {  
        num = "0" + num;  
        len++;  
    }  
    return num;  
} 

/**
 * 有疑问
 * @param callback 回调函数
 **/
function findMaxCaseBasicInfo(callback){
	db.transaction(function (tx) { 
		tx.executeSql('SELECT CASE_NUMBER FROM CASE_BASICINFO WHERE  CREATE_TIME = (SELECT MAX(CREATE_TIME) FROM CASE_BASICINFO)', [], 
		function (tx, results){
			var len = results.rows.length, i; 
			var data=new Array();
			for (i = 0; i < len; i++){ 
				var number={};
				number.caseNumber = results.rows.item(i).CASE_NUMBER; 
				data.push(number);
		    }
			if (typeof(callback) == 'function')
	            callback();
	    }, 
	    function(tx, ex) {
            message("查询案件登记最新时间失败");
        });  
	});
}


/**
 * 插入测试数据
 **/
function saveCaseBasicInfoTest(data){
	db.transaction(function (tx) {
		tx.executeSql("INSERT INTO CASE_BASICINFO (CASE_NUMBER,CASE_STEP,EXECUTION_TYPE,FILLING_DATE,FILLING_PLACE,PARTY,STAFF_1,STAFF_2,ZFML,ORGAN_ID) VALUES (?,?,?,?,?,?,?,?,?,?)",
    			[data.caseNumber,data.caseStep,data.executionType,data.fillingDate,
    			 data.fillingPlace,data.party,
    			 data.staff1,data.staff2,data.zfml,data.organId],
       function (tx, results) {
    	    message("案件保存成功");
       },
	   function (tx, error) {
    	    message("案件保存失败");
	   });
    });
}